/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
void InsertCharacter()
{
   Game::ObjParams op;
   op.type (true, "OBJ_PLAYER");
   op.mesh (true, ChrData.mesh);
   op.align(true, ALIGN_NONE, ALIGN_NONE, ALIGN_NONE);
   if(Game::Obj *obj=Game::World.objCreateNear(op, Matrix(ChrData.scale, ChrData.pos())))
   {
      if(Player *player=CAST(Player, obj))player->create(ChrData);
   }
}
/******************************************************************************/
Bool BackgroundLoader::BackgroundLoad(Thread &thread)
{
   ThreadMayUseGPUData();
   Game::World.update(Cam.at);
   return false;
}
/******************************************************************************/
void BackgroundLoader::del()
{
   Game::World.updateBreak();
   thread.del();
   image=NULL;
}
void BackgroundLoader::start()
{
   image="gfx/logo/esenthel.gfx";
   thread.create(BackgroundLoad);
   Renderer.setFade(0.5f, true);
}
/******************************************************************************/
Bool BackgroundLoader::update()
{
   if(!thread.created())return false;
   customUpdate();
   return true;
}
Bool BackgroundLoader::draw()
{
   if(!thread.created())return false;
   customDraw();
   if(!thread.active()) // finished loading
   {
      thread.del();
      image=NULL;
      Renderer.setFade(1.0f);

      InsertCharacter();
   }
   return true;
}
/******************************************************************************/
void BackgroundLoader::customUpdate()
{
   Time.wait(1000/24);
}
void BackgroundLoader::customDraw()
{
   D.clear(BLACK); if(image)image->drawFs(ColorAlpha(0.5));
   D.text (0, -D.h()+0.2f, S+"Loading...");
   D.rect (WHITE, Rect_LD(-D.w(), -D.h(), D.w()*2*Game::World.updateProgress(), 0.01f));
}
/******************************************************************************/
